﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using School.Entities;

namespace EF.WebApi.Controllers;

/// <summary>
/// 选课管理
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class EnrollmentsController : ControllerBase {
    private readonly SchoolContext _context;

    public EnrollmentsController (SchoolContext context) {
        _context = context;
    }

    /// <summary>
    /// 查看选课列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Enrollments>>> GetEnrollments () {
        return await _context.Enrollments
            //.Include(x => x.Student)
            //.Include(x => x.Course)
            //.Where(x=>x.Include(x => x.Student).Include(x => x.Student))
            .ToListAsync();
    }

    /// <summary>
    /// 查看某条选课信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ActionResult<Enrollments>> GetEnrollments (int id) {
        var enrollments = await _context.Enrollments.FindAsync(id);

        if (enrollments == null) {
            return NotFound();
        }

        return enrollments;
    }

    /// <summary>
    /// 修改某条选课记录
    /// </summary>
    /// <param name="id"></param>
    /// <param name="enrollments"></param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public async Task<IActionResult> PutEnrollments (int id, Enrollments enrollments) {
        if (id != enrollments.Id) {
            return BadRequest();
        }

        _context.Entry(enrollments).State = EntityState.Modified;

        try {
            await _context.SaveChangesAsync();
        } catch (DbUpdateConcurrencyException) {
            if (!EnrollmentsExists(id)) {
                return NotFound();
            } else {
                throw;
            }
        }

        return NoContent();
    }

    /// <summary>
    /// 添加一条选课记录
    /// </summary>
    /// <param name="enrollments"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ActionResult<Enrollments>> PostEnrollments (Enrollments enrollments) {
        _context.Enrollments.Add(enrollments);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetEnrollments", new { id = enrollments.Id }, enrollments);
    }

    /// <summary>
    /// 删除某条选课记录
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteEnrollments (int id) {
        var enrollments = await _context.Enrollments.FindAsync(id);
        if (enrollments == null) {
            return NotFound();
        }

        _context.Enrollments.Remove(enrollments);
        await _context.SaveChangesAsync();

        return NoContent();
    }

    /// <summary>
    /// 是否存在某条选课记录
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private bool EnrollmentsExists (int id) {
        return _context.Enrollments.Any(e => e.Id == id);
    }
}
